<template>
  <div>
    <el-form ref="deptForm" label-width="120px" :rules="rules" :model="form">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in list" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>
<script>
import { getEmployeeSimple } from '@/api/employee'
import { addDepartment, getDepartmentDetail, updateDepartmentById } from '@/api/department'
// import tree from '@/api/tree'
export default {
  props: {
    id: {
      required: true,
      type: String
    },
    isEdit: {
      required: true,
      type: Boolean
    },
    originList: {
      required: true,
      type: Array
    }
  },
  data() {
    // 自定义校验规则
    // 校验编码是否重复
    const validCode = (rule, value, callback) => {
      let existCode = this.originList.map(item => item.code)
      console.log('existCode', existCode)
      // 判断编辑状态
      if (this.isEdit) {
        // 如果item的id与当前id不相等,把当前部门的编码移除
        existCode = this.originList.filter(item => item.id !== this.id).map(item => item.code)
        console.log(existCode)
      }
      if (existCode.includes(value)) {
        callback(new Error('该部门编码已存在'))
      }
      callback()
    }
    // 校验部门名称是否重复
    const validName = (rule, value, callback) => {
      // 当前子部门的pid等于父部门的id
      let existName = this.originList.filter(item => item.pid === this.id).map(item => item.name)
      // console.log('existName', existName)
      // 兄嘚部门
      if (this.isEdit) {
        // 找到当前编辑的部门对象,拿到pid(通过find查找,返回一个对象)
        const pid = this.originList.find(item => item.id === this.id).pid
        // 拿到所有匹配的pid,再排除自己
        existName = this.originList.filter(item => item.pid === pid).filter(item => item.pid !== this.id).map(item => item.name)
        console.log('existName', existName)
      }
      if (existName.includes(value)) {
        // 存在,提示用户
        callback(new Error('该部门名称已存在'))
      }
      callback()
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      list: [], // 保存负责人信息
      // 表单验证
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadEmployeeSimple()
    if (this.isEdit) {
      this.loadDepartmentDetail()
    }
  },
  methods: {
    // 确定
    hSubmit() {
      this.$refs.deptForm.validate((valid) => {
        if (!valid) return
        this.isEdit ? this.hEdit() : this.hAdd()
      })
    },
    // 取消
    hCancel() {
      this.$emit('close')
    },
    // 加载部门负责人
    async  loadEmployeeSimple() {
      try {
        const res = await getEmployeeSimple()
        console.log('res', res)
        this.list = res.data
        console.log(this.list)
      } catch (error) {
        console.log(error)
      }
    },
    // 新增子部门
    async  hAdd() {
      try {
        const res = await addDepartment({ pid: this.id, ...this.form })
        console.log('res', res)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑子部门
    async  hEdit() {
      const res = await updateDepartmentById(this.form)
      console.log(res)
      if (res.code !== 10000) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.$emit('success')
    },
    // 获取部门详情
    async loadDepartmentDetail() {
      try {
        const res = await getDepartmentDetail(this.id)
        console.log('res', res)
        if (res.code === 10000) {
          this.form = res.data
        }
      } catch (error) {
        console.log(error)
      }
    }

  }
}
</script>
